import datetime
import time
from sys import executable

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.wait import WebDriverWait
# from webdriver_manager.chrome import ChromeDriverManager
# from webdriver_manager.drivers.edge import EdgeChromiumDriver

#编写自动化脚本，具体步骤
#1.打开浏览器----驱动管理
# ChromeIns=ChromeDriverManager().install()
# options = webdriver.ChromeOptions()
# options.add_argument("--remote-allow-origins=*")
edge_driver_path = r"D:\driver\edgedriver_win64\msedgedriver.exe"
service = Service(edge_driver_path)
###创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=service,options=options)
driver=webdriver.Edge(service=service)
time.sleep(2)
#2.输入百度网址：https://www.baidu.com（网址一定要完整，不要写成www.baidu.com)
driver.get("https://www.baidu.com/")
time.sleep(2)
# #3.找到百度输入框，输入关键词“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# time.sleep(2)
# #4.点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)

#定位元素
# ret=driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)

#点击事件
#查找元素+点击元素
#driver.find_element(By.CSS_SELECTOR,"#su").click()
# driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title").click()
# time.sleep(2)

#输入文本
# ele=driver.find_element(By.CSS_SELECTOR,"#kw")
# ele.send_keys("比特就业课")
# time.sleep(2)

#获取文本信息
# text=driver.find_element(By.CSS_SELECTOR,"#title-content > span.title-content-title")
# time.sleep(2)
# print(text.text)

#获取百度一下按钮上的字
# # text=driver.find_element(By.CSS_SELECTOR,"#su").text   #这里获取不到对应的“百度一下”属性值
# text=driver.find_element(By.CSS_SELECTOR,"#su").get_attribute("value")
# print(text)
# # assert text == "百度一下"

# #获取标题和页面
# title=driver.title
# url=driver.current_url
# print(title)
# print(url)
#
# #跳转页面并获取标题和页面
# print("before:"+driver.title)#百度首页的标题
# print("before:"+driver.current_url)#百度首页的URL
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# #获取当前页面的句柄---第一个标签页
# curHandle = driver.current_window_handle
# #获取所有句柄
# allHandle = driver.window_handles
# #遍历所有的句柄，切换到新的页面
# for handle in allHandle:
#     if handle != curHandle:
#         # 切换句柄
#         # driver.switch_to_window(handle)
#         driver.switch_to.window(handle)
#
# #测试跳转结果
# print("after:"+driver.title)#百度图片的标题
# print("after:"+driver.current_url)#百度图片的url

#不进行窗口跳转的话就可以直接打印出结果
# print("before:"+driver.title)#百度图片的标题
# print("before:"+driver.current_url)#百度图片的URL
# driver.find_element(By.CSS_SELECTOR,"#wrapper_main_box > div.wrapper_userinfo_box > div.bidu_top_user_info > div.sites > a:nth-child(2)").click()
# time.sleep(3)
# print("after:"+driver.title)#hao123的标题
# print("after:"+driver.current_url)#hao123的url

#截图
#添加屏幕截图
# filename = "./images/test.png"
# driver.save_screenshot(filename)

#进阶版本：每次生成的图片文件都不一样
#百度首页
# #autotest-2024-05-08-104723.png
# filename="autotest-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
# # #./image/sautotest-2024-05-08-104723.png
# driver.save_screenshot("./images/"+filename)

#打开新的标签页：百度图片
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
#
# time.sleep(3)
# driver.close()
#
# #3秒后关闭浏览器
# time.sleep(3)

#5.关闭浏览器
driver.quit()